package cn.com.anysdk.starter.config.redis;

import cn.com.anysdk.redis.api.IRedisService;
import cn.com.anysdk.redis.config.AliyunRedisConfig;
import cn.com.anysdk.redis.config.RedisConfigProperties;
import cn.com.anysdk.redis.event.RedisEventManager;
import cn.com.anysdk.redis.impl.AliyunRedisServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * 阿里云Redis自动配置类
 * 当配置anysdk.redis.aliyun.enabled=true时启用
 */
@Configuration
@EnableConfigurationProperties(AliyunRedisProperties.class)
@ConditionalOnProperty(prefix = "anysdk.redis.aliyun", name = "enabled", havingValue = "true")
public class AliyunRedisAutoConfiguration {
    
    private static final Logger log = LoggerFactory.getLogger(AliyunRedisAutoConfiguration.class);
    
    /**
     * 创建阿里云Redis配置
     */
    @Bean
    public AliyunRedisConfig aliyunRedisConfig(AliyunRedisProperties properties) {
        validateProperties(properties);
        log.info("创建阿里云Redis配置，端点: {}:{}", 
                properties.getEndpoint(), properties.getPort());
        
        AliyunRedisConfig config = new AliyunRedisConfig();
        
        // 基础连接配置
        config.setEndpoint(properties.getEndpoint());
        config.setPort(properties.getPort());
        config.setAccessKeyId(properties.getAccessKeyId());
        config.setAccessKeySecret(properties.getAccessKeySecret());
        config.setSecurityToken(properties.getSecurityToken());
        config.setPassword(properties.getPassword());
        config.setDatabase(properties.getDatabase());
        
        return config;
    }

    
    /**
     * 创建Redis配置属性
     */
    @Bean
    public RedisConfigProperties redisConfigProperties(AliyunRedisConfig aliyunRedisConfig) {
        RedisConfigProperties configProperties = new RedisConfigProperties();
        configProperties.setEnvironment("aliyun");
        
        // 创建阿里云配置包装类
        RedisConfigProperties.AliyunConfig aliyunConfigWrapper = new RedisConfigProperties.AliyunConfig();
        aliyunConfigWrapper.setConfig(aliyunRedisConfig);
        configProperties.setAliyun(aliyunConfigWrapper);
        
        return configProperties;
    }

    @Bean
    public RedisEventManager redisEventManager() {
        return new RedisEventManager();
    }

    /**
     * 创建阿里云Redis服务
     */
    @Bean(name = "aliyunRedisService")
    public IRedisService aliyunRedisService(RedisConfigProperties configProperties, RedisEventManager eventManager) {
        log.info("创建阿里云Redis服务实例");
        return new AliyunRedisServiceImpl(configProperties, eventManager);
    }
    
    /**
     * 验证配置属性
     */
    private void validateProperties(AliyunRedisProperties properties) {
        if (!StringUtils.hasText(properties.getEndpoint())) {
            throw new IllegalArgumentException("阿里云Redis端点不能为空");
        }
    }

}